package org.shock.ss.sms.action.system;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import org.shock.ss.sms.service.system.NoticeService;
import org.shock.ss.sms.service.system.impl.NoticeServiceImpl;
import org.shock.ss.sms.bean.Page;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.bean.system.Notice;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.utils.ParamsUtil;
import org.shock.ss.sms.commons.utils.ReflexUtils;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.system.impl.NoticeDaoImpl;

import org.shock.ss.sms.action.BaseAction;

public class NoticeAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private NoticeService ns = new NoticeServiceImpl((NoticeDaoImpl) DaoFactory
			.getDao("NoticeDao"));
	private Notice bean = new Notice();
	private List<Notice> noticelist;
	private long times;
	private long id;

	/**
	 * 分页显示系统公告
	 * 
	 * @return
	 * @throws Exception
	 */
	/** 公告列表 * */
	@RightAnno( rightNote = "公告列表")
	public String noticeList() throws Exception {
		if (!Util.isEmpty(bean.getTitle())) {
			sqladd.append(" and title like '%"
					+ Util.nulltoempty(bean.getTitle()) + "%'");
		}
		if (!Util.isEmpty(bean.getReleaser())) {
			sqladd.append(" and releaser like '%"
					+ Util.nulltoempty(bean.getReleaser()) + "%'");
		}

		/** Pager bean 封装 list 和 总数 */
		Pager pager = new Pager();
		/** 分页的参数处理 */
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		/** 获得总数语句 */
		String sqlcount = "select count(n.id) from notice n  where 1=1 "
				+ sqladd;
		/** 分页语句 */
		String sql = "select id,title,content,releasedate,releaser,times from notice where 1=1"
				+ sqladd;
		/** 获得pager */
		pager = ns.list("desc", "releasedate desc,id", pageno, rownum, sqlcount, sql);
		noticelist = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		if ("indexpages".equals(action)) {
			return "indexpages";
		}
		return SUCCESS;
	}

	/**
	 * 查看公告
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "查看公告")
	public String lookNotice() throws Exception {
		String sql = "select id,title,content,releasedate,releaser,times from notice where id="
				+ id;
		Map map = ns.view(sql);
		BeanUtils.populate(bean, map);
		bean.setTimes(bean.getTimes() + 1);
		ns.update(bean);
		return SUCCESS;
	}

	/**
	 * 进入添加页面
	 * 
	 * @return
	 */
	@RightAnno(rightValue = "addNotice", rightNote = "添加公告", islog = true)
	public String addNotice() {
		return SUCCESS;
	}

	/**
	 * 提交添加的公告信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addNoticeSub() throws Exception {
		bean.setReleasedate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
				.format(new java.util.Date()));
		bean.setReleaser(getUserInfor().getYgxm());
		ns.add(bean);
		return SUCCESS;
	}

	/**
	 * 进入修改公告页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightValue = "updateNotice", rightNote = "修改公告", islog = true)
	public String updateNotice() throws Exception {
		String sql = "select id,title,content,releasedate,releaser,times from notice where id="
				+ id;
		Map map = ns.view(sql);
		BeanUtils.populate(bean, map);
		return SUCCESS;
	}

	/**
	 * 提交修改
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "修改公告", islog = true)
	public String updateNoticeSub() throws Exception {
		bean.setContent(bean.getContent() + " ");
		ns.update(bean);
		prjlog.setContent("修改成功");
		prjlog.setBgcontent(ReflexUtils.rwritertoString(bean));
		return SUCCESS;
	}

	@RightAnno(rightValue = "deleteNotice", rightNote = "删除公告", islog = true)
	public String deleteNotice() throws Exception {
		bean.setId(id);
		ns.del(bean);
		return SUCCESS;
	}

	public NoticeService getNs() {
		return ns;
	}

	public void setNs(NoticeService ns) {
		this.ns = ns;
	}

	public Notice getBean() {
		return bean;
	}

	public void setBean(Notice bean) {
		this.bean = bean;
	}

	public List<Notice> getNoticelist() {
		return noticelist;
	}

	public void setNoticelist(List<Notice> noticelist) {
		this.noticelist = noticelist;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getTimes() {
		return times;
	}

	public void setTimes(long times) {
		this.times = times;
	}
}
